package com.ksd.api.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ksd.api.bean.SynchDataRequest;
import com.ksd.api.bean.SynchDataResponse;
import com.ksd.api.utils.HttpServletUtil;
import com.ksd.common.core.controller.BaseController;
import com.ksd.common.utils.StringUtils;
import com.ksd.system.domain.SysData;
import com.ksd.system.domain.SysVersion;
import com.ksd.system.service.ISysDataService;
import com.ksd.system.service.ISysVersionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class checkDataZip extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(checkDataZip.class);
    @Autowired
    private ISysVersionService sysVersionService;
    @Autowired
    private ISysDataService sysDataService;

    @RequestMapping(value = "/api/cloud/synchContentDataZip", method = RequestMethod.POST)
    @ResponseBody
    public String checkDataZip(HttpServletRequest httpServletRequest) {
        System.out.println("===================播控后台管理系统同步数据导入信息===============");
        StringBuffer requestURL = httpServletRequest.getRequestURL();

        SynchDataResponse response = new SynchDataResponse();
        String req = HttpServletUtil.getReqByInputStream(httpServletRequest);
        ObjectMapper mapper = new ObjectMapper();
        SynchDataRequest request = null;
        try {
            request = mapper.readValue(req, SynchDataRequest.class);
            if (StringUtils.isNotNull(request)) {
                String operatorKey = request.getOperatorKey();
                System.out.println("待同步的数据内容导入的运营商标识:"+operatorKey);
                SysVersion sysVersion = new SysVersion();
                if (StringUtils.isNotNull(operatorKey)) {
                    sysVersion.setOperatorKey(operatorKey);
                }
                List<SysVersion> sysVersions = sysVersionService.selectSysVersionList(sysVersion);
                if (StringUtils.isNull(sysVersions)) {
                   return response.setError("该运营商机顶盒在云服务器上暂时还未注册,请确认运营商:"+operatorKey);
                }
                SysData sysData = new SysData();
                List<SysData> sysDatas = sysDataService.selectSysDataList(sysData);
                System.out.println("待升级同步的数据同步个数:"+sysDatas.size());
                if (sysDatas.size() > 0) {
                    sysData = sysDatas.get(0);
                    String url = sysData.getUrl();
                    if (StringUtils.isNotEmpty(url)) {
                        sysVersion = sysVersions.get(0);
                        System.out.println("查询到云服务上该运营商有需要同步的数据导入压缩包:"+url);
                        response.setZipName(sysData.getName());
                        response.setZipPath(sysData.getUrl());
//                        response.setFileType(sysData.getFileType());
                        return response.setSuccess(response);
                    }
                    return response.setError("该运营商机顶盒在云服务器上暂时没有待同步内容,请先在云服务上进行数据导入");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return response.setError(e.getMessage());
        }
        return response.setError("error");
    }
}